import java.io.BufferedReader;
import java.io.InputStreamReader;

import edu.ucla.cs.typecast.discovery.ServiceAdvertisement;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryClient;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryListener;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryManager;
import edu.ucla.cs.typecast.discovery.ServiceDiscoveryService;
import edu.ucla.cs.typecast.discovery.ServiceTemplate;


public class Discovery {
    public static void main(String[] args) { 
        try { 
            Class type = Class.forName(args[0]); 
            Class[] types = new Class[] { type }; 
            ServiceTemplate template = new ServiceTemplate(types); 
            ServiceDiscoveryListener listener = new ServiceDiscoveryListener() { 
                public void notify(ServiceAdvertisement serviceInfo) { 
                    System.out.print("Discovered Services: "  +  
                            "Host=" + serviceInfo.getHost() + ", GUID=" + serviceInfo.getGUID()); 
                    System.out.print(", Type=["); 
                    for(int i=0; i<serviceInfo.getTypes().length; i++) { 
                        System.out.print(serviceInfo.getTypes()[i].getName() + " "); 
                    }
                    System.out.print("], EndPoint=["); 
                    for(int i=0; i<serviceInfo.getEndPoints().length; i++) { 
                        System.out.print(serviceInfo.getEndPoints()[i] + " "); 
                    }
                    System.out.println("]"); 
                }
            }; 
            ServiceDiscoveryManager.getInstance().addDiscoveryListener(template, listener); 
            ServiceDiscoveryClient client = new ServiceDiscoveryClient(); 
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
            while(true){ 
                client.discover(types, template); 
                reader.readLine(); 
            }                
        } catch(Throwable t) { 
            t.printStackTrace(); 
        }
    }
}
